package com.example.dolgi;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
 * @author dark-wizard
 * 
 * Simple class, which just represents table in Database
 * extends {@link SQLiteOpenHelper}
 */
public class SQLLiteManager extends SQLiteOpenHelper {
	//constants for implementation DB requests
	public static final String DB_NAME = "test.db";
	public static final int DB_VERSION = 1;
	
	public static final String TABLE_NAME = "testDB"; 
	//all started with COLUMN_ is representaion of columns in database
	public static final String COLUMN_USERNAME = "username";
	public static final String COLUMN_DEBT = "debt";
	public static final String COLUMN_ID = "_id";
	
	/**
	 * creates new DB from super(context, DB name, cursor factory, database version)
	 * @param context
	 */
	
	public SQLLiteManager(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}
	/**
	 * creating our table with direct SQL request
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("create table "+TABLE_NAME+" ("+COLUMN_ID+" integer primary key autoincrement, "+COLUMN_USERNAME+" text not null, "+COLUMN_DEBT+" text not null)");
	}
	/**
	 * Function is called when database needs upgrade
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int old, int newVersion) {
		// TODO Auto-generated method stub
		Log.w(SQLLiteManager.class.getName(), "Upgrading from version " + old + " to version "+newVersion+"...");
		db.execSQL("drop table if not exists "+TABLE_NAME);
		onCreate(db);
	}
}